package com.bw.ods;

import com.alibaba.fastjson.JSON;
import com.alibaba.ververica.cdc.connectors.mysql.MySQLSource;
import com.alibaba.ververica.cdc.debezium.DebeziumSourceFunction;
import com.bw.utils.JsonDeserializationSchemaUtil;
import com.bw.utils.MyKafkaUtil;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;

public class Test1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DebeziumSourceFunction<String> source = MySQLSource.<String>builder().hostname("hadoop102").port(3306).databaseList("tms01") // set captured database
                //.tableList("yourDatabaseName.yourTableName") // set captured table
                .username("root").password("123456").deserializer(new JsonDeserializationSchemaUtil()) // converts SourceRecord to JSON String
                .build();

        DataStreamSource<String> stream = env.addSource(source);
        //过滤data为空的数据
        SingleOutputStreamOperator<String> filterDS = stream.filter(x -> JSON.parseObject(x).getJSONObject("data") != null && JSON.parseObject(x).getJSONObject("data").size() > 0);
        //写入kafka:ods-tms-topic

        filterDS.print();
        filterDS.addSink(MyKafkaUtil.getKafkaProducer("tms_ods_yk5"));
        env.execute();
    }
}
